package com.shockweb.common.security;


public class SM4Utils {
      
    /**
     * ECB方式加密
     * @param secretKey
     * @param data
     * @return
     * @throws SecurityException
     */
    public static byte[] encrypt_ECB(byte[] secretKey,byte[] data)  throws SecurityException
    {  
        SM4_Context ctx = new SM4_Context();  
        ctx.isPadding = true;  
        ctx.mode = SM4.SM4_ENCRYPT;  
        SM4 sm4 = new SM4();  
        sm4.sm4_setkey_enc(ctx, secretKey);  
        byte[] encrypted = sm4.sm4_crypt_ecb(ctx, data);  
        return encrypted;  
    } 
    
    /**
     * ECB方式加密
     * @param secretKeyBase64
     * @param data
     * @param charset
     * @return
     * @throws SecurityException
     */
    public static String encrypt_ECB(String secretKeyBase64,String data,String charset)  throws SecurityException
    {  
    	if(data==null) {
    		return null;
    	}
    	try {
    		return Utils.base64EncodeString(encrypt_ECB(Utils.base64Decode(secretKeyBase64),data.getBytes(charset)));
    	}catch(SecurityException e) {
    		throw e;
        }catch(Exception e) {
        	throw new SecurityException("encryptData_ECB异常",e);
        }
    } 
    

    /**
     * ECB方式解密
     * @param secretKey
     * @param data
     * @return
     * @throws SecurityException
     */
    public static byte[] decrypt_ECB(byte[] secretKey,byte[] data)  throws SecurityException
    {  
        SM4_Context ctx = new SM4_Context();  
        ctx.isPadding = true;  
        ctx.mode = SM4.SM4_DECRYPT;  
        SM4 sm4 = new SM4();  
        sm4.sm4_setkey_dec(ctx, secretKey);  
        byte[] decrypted = sm4.sm4_crypt_ecb(ctx, data);  
        return decrypted;  
    }
    
    
    /**
     * ECB方式解密
     * @param secretKeyBase64
     * @param encryptedDataBase64
     * @param charset
     * @return
     * @throws SecurityException
     */
    public static String decrypt_ECB(String secretKeyBase64,String encryptedDataBase64,String charset)  throws SecurityException
    {  
    	if(encryptedDataBase64==null) {
    		return null;
    	}
    	byte[] data = decrypt_ECB(Utils.base64Decode(secretKeyBase64),Utils.base64Decode(encryptedDataBase64));
    	if(data==null) {
    		return null;
    	}else {
    		try {
    			return new String(data,charset);
	        }catch(Exception e) {
	        	throw new SecurityException("decryptData_ECB异常",e);
	        }
    	}
    }
    
    

    /**
     * CBC方式加密
     * @param secretKey
     * @param iv
     * @param data
     * @return
     * @throws SecurityException
     */
    public static byte[] encrypt_CBC(byte[] secretKey,byte[] iv,byte[] data) throws SecurityException  
    {  

        SM4_Context ctx = new SM4_Context();  
        ctx.isPadding = true;  
        ctx.mode = SM4.SM4_ENCRYPT;  
        SM4 sm4 = new SM4();  
        sm4.sm4_setkey_enc(ctx, secretKey);  
        byte[] encrypted = sm4.sm4_crypt_cbc(ctx, iv, data);  
        return encrypted;  
    }  
    
    /**
     * CBC方式加密
     * @param secretKeyBase64
     * @param ivBase64
     * @param encryptedDataBase64
     * @param charset
     * @return
     * @throws SecurityException
     */
    
    public static String encrypt_CBC(String secretKeyBase64,String ivBase64,String data,String charset)  throws SecurityException
    {  
    	if(data==null) {
    		return null;
    	}
    	try {
    		return Utils.base64EncodeString(encrypt_CBC(Utils.base64Decode(secretKeyBase64),Utils.base64Decode(ivBase64),data.getBytes(charset)));
    	}catch(SecurityException e) {
    		throw e;
        }catch(Exception e) {
        	throw new SecurityException("encryptData_CBC异常",e);
        }
    }
    
    /**
     * CBC方式解密
     * @param secretKey
     * @param iv
     * @param data
     * @return
     * @throws SecurityException
     */
    public static byte[] decrypt_CBC(byte[] secretKey,byte[] iv,byte[] data)  throws SecurityException
    {  
            SM4_Context ctx = new SM4_Context();  
            ctx.isPadding = true;  
            ctx.mode = SM4.SM4_DECRYPT;  
            SM4 sm4 = new SM4();  
            sm4.sm4_setkey_dec(ctx, secretKey);  
            byte[] decrypted = sm4.sm4_crypt_cbc(ctx, iv, data);  
            return decrypted;  
    }  
    
    /**
     * CBC方式解密
     * @param secretKeyBase64
     * @param ivBase64
     * @param encryptedDataBase64
     * @param charset
     * @return
     * @throws SecurityException
     */
    public static String decrypt_CBC(String secretKeyBase64,String ivBase64,String encryptedDataBase64,String charset)  throws SecurityException
    {  
    	if(encryptedDataBase64==null) {
    		return null;
    	}
    	byte[] data = decrypt_CBC(Utils.base64Decode(secretKeyBase64),Utils.base64Decode(ivBase64),Utils.base64Decode(encryptedDataBase64));
    	if(data==null) {
    		return null;
    	}else {
    		try {
    			return new String(data,charset);
	        }catch(Exception e) {
	        	throw new SecurityException("decryptData_CBC异常",e);
	        }
    	}
    }

    

    

}
